package top.lyjwn.todo.weinxin.api.url;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import top.lyjwn.todo.common.utils.http.HttpUtil;

/**
 * 微信支付的API
 * 
 * @author luoyujie
 *
 */
public class WXPayUrl {

	private final static Logger logger = LoggerFactory.getLogger(WXPayUrl.class);

	/**
	 * 统一下单
	 */
	public static String UNIFIED_ORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";

	/**
	 * 统一下单,备用接口"https://api.mch.weixin.qq.com/pay/unifiedorder"
	 */
	private final static List<String> UNIFIED_ORDER_STANDBY = new ArrayList<String>();

	/**
	 * 查询订单
	 */
	public static String QUERY_ORDER = "https://api.mch.weixin.qq.com/pay/orderquery";

	/**
	 * 查询订单,备用接口https://api2.mch.weixin.qq.com/pay/orderquery
	 */
	private final static List<String> QUERY_ORDER_STANDBY = new ArrayList<String>();

	/**
	 * 关闭订单
	 */
	public static String CLOSE_ORDER = "https://api.mch.weixin.qq.com/pay/closeorder";

	/**
	 * 关闭订单,备用接口https://api2.mch.weixin.qq.com/pay/closeorder
	 */
	private final static List<String> CLOSE_ORDER_STANDBY = new ArrayList<String>();;

	/**
	 * 申请退款
	 * 
	 * 请求需要双向证书
	 */
	public static String APPLY_REFUND = "https://api.mch.weixin.qq.com/secapi/pay/refund";

	/**
	 * 查询退款
	 */
	public static String QUERY_REFUND = "https://api.mch.weixin.qq.com/pay/refundquery";

	/**
	 * 下载对账单
	 */
	public static String DOWNLOAD_BILL = "https://api.mch.weixin.qq.com/pay/downloadbill";

	/**
	 * 下载资金账单
	 * 
	 * 请求需要双向证书
	 */
	public static String DOWNLOAD_FUND_FLOW = "https://api.mch.weixin.qq.com/pay/downloadfundflow";

	/**
	 * 交易保障
	 * 
	 * 上报api耗时接口
	 * 
	 * 应用场景:商户在调用微信支付提供的相关接口时，会得到微信支付返回的相关信息以及获得整个接口的响应时间。为提高整体的服务水平，协助商户一起提高服务质量，微信支付提供了相关接口调用耗时和返回信息的主动上报接口，微信支付可以根据商户侧上报的数据进一步优化网络部署，完善服务监控，和商户更好的协作为用户提供更好的业务体验。
	 */
	public static String REPORT_API_ELAPSED = "https://api.mch.weixin.qq.com/payitil/report";

	/**
	 * 批量查询评论
	 */
	public static String BATCH_QUERY_COMMENT = "https://api.mch.weixin.qq.com/billcommentsp/batchquerycomment";

	/**
	 * 轮询
	 */
	public static void polling() {
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					apiDetection();
					try {
						Thread.sleep(1000 * 60 * 60);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		thread.start();
		logger.info("微信支付接口监测程序启动...");
	}

	/**
	 * 检测接口是否故障，故障则转换为备用接口
	 */
	private static void apiDetection() {
		unifiedOrderDetection();
		queryOrdeRDetection();
		closeOrdeRDetection();
	}

	/**
	 * 统一下单检测
	 */
	private static void unifiedOrderDetection() {
		if (!UNIFIED_ORDER_STANDBY_load) {
			UNIFIED_ORDER_STANDBY.add("https://api2.mch.weixin.qq.com/pay/unifiedorder");
		}
		if (HttpUtil.request(UNIFIED_ORDER, "GET") == null) {
			for (String url : UNIFIED_ORDER_STANDBY) {
				if (HttpUtil.request(url, "GET") != null) {
					UNIFIED_ORDER = url;
				}
			}
		}
	}

	/**
	 * 统一下单接口是否加载
	 */
	private static boolean UNIFIED_ORDER_STANDBY_load = false;

	/**
	 * 查询订单检测
	 */
	private static void queryOrdeRDetection() {
		if (!QUERY_ORDER_STANDBY_load) {
			QUERY_ORDER_STANDBY.add("https://api2.mch.weixin.qq.com/pay/orderquery");
		}
		if (HttpUtil.request(QUERY_ORDER, "GET") == null) {
			for (String url : QUERY_ORDER_STANDBY) {
				if (HttpUtil.request(url, "GET") != null) {
					QUERY_ORDER = url;
				}
			}
		}
	}

	/**
	 * 查询订单接口是否加载
	 */
	private static boolean QUERY_ORDER_STANDBY_load = false;

	/**
	 * 关闭订单检测
	 */
	private static void closeOrdeRDetection() {
		if (!CLOSE_ORDER_STANDBY_load) {
			CLOSE_ORDER_STANDBY.add("https://api2.mch.weixin.qq.com/pay/closeorder");
		}
		if (HttpUtil.request(CLOSE_ORDER, "GET") == null) {
			for (String url : CLOSE_ORDER_STANDBY) {
				if (HttpUtil.request(url, "GET") != null) {
					CLOSE_ORDER = url;
				}
			}
		}
	}

	/**
	 * 关闭订单接口是否加载
	 */
	private static boolean CLOSE_ORDER_STANDBY_load = false;

}
